package cn.darkhorse.messystem.service.impl;

import cn.darkhorse.messystem.service.MesSysDeptServie;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  mao
 * @since 2023-1-3
 */
@Service
public class MesSysDeptServieImpl implements MesSysDeptServie {
    @Resource
    private ISysDeptService sysDeptService;
    /**
     * 获取部门全路径
     * @param type
     * @return
     */
    @Override
    public Map<Long, String> getDeptS(Integer type) {
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        Map<Long, String> deptMap = new HashMap<>();
        for (SysDept dept :
                sysDepts) {
            deptMap.put(dept.getDeptId(), dept.getDeptName());
        }

        if(null != type && type.equals(2)) {
            return  deptMap;
        }

        Map<Long, String> resultMap = new HashMap<>();
        for (SysDept dept :
                sysDepts) {
            if (dept.getParentId().equals(0L)) {
                continue;
            }
            String[] split = (dept.getAncestors() + "," + dept.getDeptId().toString()).split(",");

            StringBuilder deptFullName = new StringBuilder();
            for (int i = 1; i < split.length; i++) {
                deptFullName.append(deptMap.get(Long.valueOf(split[i]))).append("/");
            }
            resultMap.put(dept.getDeptId(),deptFullName.substring(0,deptFullName.length() - 1).toString());
        }
        return resultMap;
    }
}
